package csu.web.mypetstore.service;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;

public class AccountService {
    private AccountDao accountDao;
    public  AccountService(){
        this.accountDao = new AccountDaoImpl();
    }

    private String msg;
    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountDao.getAccountByUsernameAndPassword(account);
    }
    public Account getAccountByPhone(String phone) {
        Account account = new Account();
        account.setPhone(phone);

        return accountDao.getAccountByPhone(account);
    }
    public boolean addUser(Account regsisterAccount){
        String username = regsisterAccount.getUsername();
        if(accountDao.findUserByUsername(username)){
            this.msg = "Username Exist!";
            return false;
        }
            accountDao.insertAccount(regsisterAccount);
            accountDao.insertAccount_2(regsisterAccount);
            accountDao.insertAccount_3(regsisterAccount);
            return true;
    }

    public String SearchUser(String username){
        if(accountDao.findUserByUsername(username)||username.equals("")){
            return "Username Exist!";
        }
        return "    ";
    }
    public String Phone(String number){
        String msg="";
        if(number == null || number.equals("")){
            msg = "Phone is empty!";
        }
        if(!number.matches("\\d{8,11}")){
            msg = "Phone is illeague!";
        }
        else {
            msg = "Phone is OK";
        }
        return msg;
    }

    public void changeUser(Account account)
    {
        accountDao.changeAccount(account);
        accountDao.changeAccount_2(account);
        accountDao.changeAccount_3(account);
        System.out.println("change成功！");
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
